package cs236369.hw5;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 * A static class to convert the xml that was created with the XMLGenerator to KML.
 * We use 3 xsl files one to bring only tasks, one to bring only teams and one to bring matches.
 *
 */

public class XALANconverter {
	
	public static final String kmlTasksPath = "/WEB-INF/kmlTasksStyle.xsl";	
	public static final String kmlTeamsPath = "/WEB-INF/kmlTeamsStyle.xsl";	
	public static final String kmlMatchesPath = "/WEB-INF/kmlMatchesStyle.xsl";	
	public static final String jsonPostsPath = "/WEB-INF/jsonPostsStyle.xsl";
	private static TransformerFactory tFactory = TransformerFactory.newInstance();
	
	/**
	 * Gets tasks in XML format and converts it to tasks in KML format
	 * @param in - the input stream that contains the tasks in XML format
	 * @param out - the output stream to which the KML will be put
	 * @param bringWhat - 0 to return tasks, 1 to return teams, 2 to return matches
	 * @param sc - the servlet context (to have access to the xsl files)
	 */
	public static void convertPostsToKML(InputStream in, OutputStream out, int bringWhat, ServletContext sc){
		if(in==null){
			System.err.println("ERROR: convertPostsToKML got a null InputStream");
		}
		String strKmlPath="";
		switch (bringWhat) {
		case 0:
			strKmlPath=kmlTasksPath;
			break;
		case 1:
			strKmlPath=kmlTeamsPath;
			break;
		case 2:
			strKmlPath=kmlMatchesPath;
			break;
		default:
			System.err.println("ERROR: convertPostsToKML got a wrong bringWhat: " + bringWhat);
			return;
		}
		try {
			Templates translet = tFactory.newTemplates(new StreamSource(sc.getResourceAsStream(strKmlPath)));
			Transformer transformer = translet.newTransformer();
			transformer.transform(new StreamSource(in),	new StreamResult(out));
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Gets posts in XML format and converts it to posts in JSON format
	 * @param in - the input stream that contains the posts in XML format
	 * @param out - the output stream to which the JSON will be put
	 * @param sc - the servlet context (to have access to the xsl files)
	 */
	public static void convertPostsToJSON(InputStream in, OutputStream out, ServletContext sc){
		if(in==null){
			System.err.println("ERROR: convertPostsToJSON got a null InputStream");
		}
		try {
			Templates translet = tFactory.newTemplates(new StreamSource(sc.getResourceAsStream(jsonPostsPath)));
			Transformer transformer = translet.newTransformer();
			transformer.setOutputProperty(OutputKeys.METHOD, "text");
			transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			transformer.transform(new StreamSource(in),	new StreamResult(out));
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}
}
